home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / lzw4w10.zip / DISPLAY.C < prev    next >
Text File  |  1994-07-05  |  4KB  |  168 lines

  1. /* display.c */
  2.  
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include "display.h"
  6. #include "ascii.h"
  7.  
  8. #define MIN(a,b) ((a<=b)?(a):(b))
  9.  
  10. /* private variables */
  11. static HWND hTheWnd = 0;
  12. static int CharHeight;       /* char height */
  13. static int CharWidth;        /* (fixed) char width */
  14. static int TheRow = 0;       /* current row */
  15. static int TheCol = 0;       /* current col */
  16. static int TopRow = 0;       /* top row in invalid rectangle */
  17. static int LeftCol= 0;       /* left col in invalid rectangle */
  18. static int RightCol = 0;     /* right col in invalid rectangle */
  19. static char Buffer[NROWS][NCOLS];  /* display buffer */
  20. static char *RowPtr[NROWS];  /* array of row pointers */
  21. static TEXTMETRIC tm;        /* text metric structure */
  22.  
  23. /*** PRIVATE functions ***/
  24.  
  25. static void DoTheScroll(void)
  26. {int Row;
  27.  int Col;
  28.  char *Ptr;
  29.  RECT rect;
  30.  /* scroll display buffer */
  31.  TheRow = NROWS-1;
  32.  Ptr = RowPtr[0];
  33.  for(Row=0;Row<NROWS-1;Row++) RowPtr[Row] = RowPtr[Row+1];
  34.  RowPtr[NROWS-1] = Ptr;
  35.  for(Col=0;Col<NCOLS;Col++) *Ptr++ = ' ';
  36.  /* scroll the display */
  37.  ScrollWindow(hTheWnd,0,0-CharHeight,NULL,NULL);
  38.  /* invalidate last row */
  39.  rect.left = 0;
  40.  rect.top  = CharHeight * (NROWS-2);
  41.  rect.right  = CharWidth * (RightCol+1);
  42.  rect.bottom = CharHeight * (NROWS-1);
  43.  InvalidateRect(hTheWnd,&rect,TRUE);
  44.  /* reset boundary */
  45.  TopRow = TheRow;
  46.  LeftCol = TheCol;
  47.  RightCol = TheCol;
  48. } /* end DoTheScroll */
  49.  
  50. /*** PUBLIC functions ***/
  51.  
  52. int FAR PASCAL GetCol(void)
  53. {return(TheCol);
  54. }
  55.  
  56. int FAR PASCAL GetRow(void)
  57. {return(TheRow);
  58. }
  59.  
  60. void FAR PASCAL DisplayInit(HWND hWnd)
  61. {int Col;
  62.  int Row;
  63.  HDC hDC;
  64.  hTheWnd = hWnd;
  65.  hDC = GetDC(hWnd);
  66.  SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  67.  GetTextMetrics(hDC,&tm);
  68.  CharHeight = tm.tmHeight + tm.tmExternalLeading;
  69.  CharWidth = tm.tmMaxCharWidth;
  70.  ReleaseDC(hWnd,hDC);
  71.  /* initialize screen buffer */
  72.  for(Row=0;Row<NROWS;Row++)
  73.    {for(Col=0;Col<NCOLS;Col++) Buffer[Row][Col] = ' ';
  74.     RowPtr[Row] = &Buffer[Row][0];
  75.    }
  76.  TheRow = 0;
  77.  TheCol = 0;
  78. } /* end DisplayInit */
  79.  
  80.  
  81. void FAR PASCAL DisplayText(char *String)
  82. {int i;
  83.  int Length;
  84.  char TheChar;
  85.  RECT rect;
  86.  /* begin */
  87.  TopRow = TheRow;
  88.  LeftCol = TheCol;
  89.  RightCol = TheCol;
  90.  Length = lstrlen(String);
  91.  for(i=0;i<Length;i++)
  92.    {TheChar = *String++;
  93.     switch(TheChar)
  94.      {case BS:
  95.         if(TheCol>0)
  96.           {*(RowPtr[TheRow]+TheCol) = ' ';
  97.            TheCol--;
  98.           }
  99.         break;
  100.       case CR:
  101.         TheCol = 0;
  102.         LeftCol = 0;
  103.         break;
  104.       case LF:
  105.         /* next line */
  106.         if(++TheRow>=NROWS) DoTheScroll();
  107.         break;
  108.       default:
  109.         /* put char into display buffer */
  110.         *(RowPtr[TheRow]+TheCol) = (char)TheChar;
  111.         /* increment 'cursor' */
  112.         if(++TheCol>=NCOLS)
  113.           {/* next line */
  114.            TheCol = 0;
  115.            LeftCol = 0;
  116.            if(++TheRow>=NROWS) DoTheScroll();
  117.           }
  118.         else RightCol++;
  119.         break;
  120.      } /* end switch */
  121.    } /* end for */
  122.  /* compute invalid rectangle */
  123.  if((TopRow!=TheRow)||(LeftCol!=TheCol)||(RightCol!=TheCol))
  124.    {rect.left = CharWidth * LeftCol;
  125.     rect.top  = CharHeight * TopRow;
  126.     rect.right  = CharWidth * (RightCol+1);
  127.     rect.bottom = CharHeight * (TheRow+1);
  128.     InvalidateRect(hTheWnd,&rect,TRUE);
  129.    }
  130. }  /* end DisplayText */
  131.  
  132.  
  133. void FAR PASCAL DisplayPaint(HDC hDC,PAINTSTRUCT *ps)
  134. {int Row;
  135.  int FirstRow;
  136.  int FirstCol;
  137.  int NbrRows;
  138.  int NbrCols;
  139.  int ColWidth;
  140.  int X;
  141.  int Y;
  142.  RECT rect;
  143.  /* compute row & col stuff */
  144.  FirstRow = ps->rcPaint.top  / CharHeight;
  145.  FirstCol = ps->rcPaint.left / CharWidth;
  146.  NbrRows = (ps->rcPaint.bottom - ps->rcPaint.top)  / CharHeight;
  147.  ColWidth = ps->rcPaint.right  - ps->rcPaint.left;
  148.  NbrCols = MIN(NCOLS,(1+ColWidth) / CharWidth);
  149.  X = ps->rcPaint.left;
  150.  /* consider each row */
  151.  for(Row=FirstRow;Row<FirstRow+NbrRows;Row++)
  152.    {/* paint part of row */
  153.     if((Row>=0)&&(Row<NROWS))
  154.       {/* good row number */
  155.        Y = CharHeight*Row;
  156.        /* compute bounding rectangle */
  157.        rect.left = X;
  158.        rect.top  = Y;
  159.        rect.right  = X + ColWidth;
  160.        rect.bottom = Y + CharHeight;
  161.        /* paint it */
  162.        SetBkMode(hDC,OPAQUE);
  163.        ExtTextOut(hDC,X,Y,ETO_OPAQUE|ETO_CLIPPED,&rect,
  164.           RowPtr[Row]+FirstCol,NbrCols,NULL);
  165.       }
  166.    }
  167. } /* end DisplayPaint */
  168.